/*
 * Created on Jan 11, 2005
 */
package org.vectrics.common.db;

import java.util.Comparator;
import org.vectrics.SystemException;

/**
 * @author mmoore
 */
public class ExpirableStartDateComparator implements Comparator {

	/**
	 * 
	 */
	public ExpirableStartDateComparator() {
		super();
	}

	/*
	 * 
	 */
	public int compare(Object object1, Object object2) {
		Expirable expirable1 = (Expirable)object1;
		Expirable expirable2 = (Expirable)object2;
		
		// Not converting to Long's leads to exceptions on the "compareTo()" where 
		// a conversion to TimeStamp from Date seems to occur.
		Long date1 = new Long(expirable1.getDateStart().getTime());
		Long date2 = new Long(expirable2.getDateStart().getTime());
		if (date1 == null)
			throw new SystemException("First Expirable has NULL start date");
		
		if (date2 == null)
			throw new SystemException("Second Expirable has NULL start date");

		int result = (date1).compareTo(date2);
		
		if (result == 0) {
			result = expirable1.toString().compareTo(expirable2.toString());
		}
		
		if (result == 0) {
			if (expirable1.getDateEnd() == null) {
				result = -1;
			}
			if (expirable2.getDateEnd() == null) {
				result = 1;
			}
		}
		
		if (result == 0)
			result = 1;
		return (result);
	}

}
